package ch.bluepenguin.sling.rest;

import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: christian
 * Date: Sep 17, 2008
 * Time: 7:55:19 PM
 * To change this template use File | Settings | File Templates.
 */
public class DefaultResponseHandler implements ResponseHandler {
    /**
     * returns whenever possible a String
     * @param responseBody
     * @return
     */
    public Object handleResponse(Object responseBody) {
        if(responseBody instanceof InputStream) {
            InputStream stream = (InputStream)responseBody;
            BufferedReader br = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            char[] buf= new char[1024];
            try {
                while( br.read(buf) != -1) {
                  buffer.append(buf);
                }
                return buffer.toString();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        return responseBody.toString();
    }

    //handles everything :)
    public boolean handlesClass(String mimetype) {
        return true;  //To change body of implemented methods use File | Settings | File Templates.
    }
    //handles everything :)
    public boolean handlesMimeType(String mimetype) {
        return true;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
